<template>
  <div>
    <!-- 面包屑到导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 输入框与添加按钮区域 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrdersList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderLists" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              ToolTipText="请输入内容"
              @click="editAddresssDialog"
            ></el-button>
            <!-- 编辑用户按钮 -->
            <el-button
              type="success"
              icon="el-icon-setting"
              size="mini"
              @click="showProgressBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改订单地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      width="30%"
      @close="edidAddressDialogClosed"
    >
      <el-form
        :model="editAddressForm"
        :rules="addAddressFormrules"
        ref="editAddressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="consignee_addr">
          <el-cascader
            v-model="editAddressForm.consignee_addr"
            :options="cityData"
            :props="cityDataProps"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detail_addr">
          <el-input v-model="editAddressForm.detail_addr"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 展示物流进度的对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressBoxDialogVisible"
      width="50%"
      @close="progressBoxDialogClosed"
    >
      <el-timeline reverse="true">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.ftime"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  data() {
    return {
      // 订单表格数据源
      orderLists: [],
      queryInfo: { query: '', pagenum: 1, pagesize: 10 },
      total: 0,
      // 地址修改对话框
      addressDialogVisible: false,
      // 物流进度对话框
      progressBoxDialogVisible: false,
      // 修改地址表单
      editAddressForm: {
        // 市区县
        consignee_addr: '',
        // 详细地址
        detail_addr: '',
      },
      progressInfo: [],
      // 表单校验规则
      addAddressFormrules: {
        consignee_addr: [
          { required: true, message: '请选择省市区', trigger: 'blur' },
        ],
        detail_addr: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
      },
      cityData: cityData,
      cityDataProps: {
        label: 'label',
        value: 'value',
        children: 'children',
      },
    }
  },
  methods: {
    // 获取订单数据
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: {
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize,
        },
      })
      if (res.meta.status !== 200) {
        this.$message.error('商品列表数据获取失败！' + res.meta.msg)
      }
      console.log(res)
      this.orderLists = res.data.goods
      this.total = res.data.total
    },
    // 监听页面pagesize改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    // 监听页码值的改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrdersList()
    },
    // 弹出地址修改对话框
    editAddresssDialog() {
      this.addressDialogVisible = true
    },
    // 监听级联下拉框的改变事件
    handleChange() {},
    // 监听修改地址对话框关闭重置表单
    edidAddressDialogClosed() {
      this.$refs.editAddressFormRef.resetFields()
    },
    // 展示物流进度对话框
    async showProgressBox() {
      const { data: res } = await this.$http.get(`/kuaidi/${1106975712662}`, {
        params: {},
      })
      if (res.meta.status !== 200) {
        this.$message.error('商品列表数据获取失败！' + res.meta.msg)
      }
      console.log(res)
      this.progressInfo = res.data
      this.progressBoxDialogVisible = true
    },
    // 监听物流对话框的关闭
    progressBoxDialogClosed() {},
  },
  created() {
    this.getOrdersList()
  },
}
</script>

<style scoped>
</style>
